package com.dminvoicemanager.reports;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.dminvoicemanager.DM_InvoiceMenu;
import com.dminvoicemanager.DM_Reports_Generator;
import com.dminvoicemanager.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class DM_Retailer_Master_Report extends Activity {
	private static final String TAG = null;
	private String DBNAME = "dminvoicemanager.db";
	private String USER_SETTINGS = "usersettings";
	String item_result_1 = "0", item_result_2 = "0", item_result_3 = "0",
			currentReportName, currentUserTemp, currentUserPrefix, UserLocCode;
	TextView txtHeadingReports;
	EditText dmdisplayrepname;

	public void BackToMainMenu(View view) {
		Intent myIntent = new Intent(view.getContext(), DM_Reports_Generator.class);
		myIntent.putExtra("TAGID", "RID/RNAME");
		myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivityForResult(myIntent, 0);
		finish();
		return;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dm_sales_summary_reporth);
		Bundle bundle = getIntent().getExtras();
		currentReportName = bundle.getString("REPORTNAME");

		txtHeadingReports = (TextView) findViewById(R.id.txtHeadingReports);
		txtHeadingReports.setText(currentReportName);
		dmdisplayrepname = (EditText) findViewById(R.id.dmdisplayrepname);
		currentuserTemp();
		DisplayStockInHandReport();
	}

	private void DisplayStockInHandReport() {
		// TODO Auto-generated method stub
		ListView lvtypedes = (ListView) findViewById(R.id.dminvoiceitemsdetlist);
		// create the grid item mapping
		String[] fromtrdes = new String[] { "item_result_1", "item_result_2",
				"item_result_3" };

		int[] totrdes = new int[] { R.id.record_1, R.id.record_2, R.id.record_3 };
		// prepare the list of all records
		List<HashMap<String, String>> fillMapstrdes = new ArrayList<HashMap<String, String>>();
		SQLiteDatabase mydb;
		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor allrowitemsdes = null;

		try {
			//String query = "SELECT IT.ItemCode,IT.ItemName,ITL.LocCode,ITL.QOH,IT.AvgPrice from fItems IT,fItemLoc ITL  Where IT.ItemCode = ITL.ItemCode group by ITL.Itemcode";
			 String query = "SELECT DebName,DebCode,TownCode from fDebtor";
			allrowitemsdes = mydb.rawQuery(query, null);
		} catch (Exception e) {
		}
		if (allrowitemsdes != null) {
			if (allrowitemsdes.getCount() > 0) {
				while (allrowitemsdes.moveToNext()) {
					// Item Name
					item_result_1 = allrowitemsdes.getString(0);
					item_result_2 = allrowitemsdes.getString(1);
					item_result_3 = allrowitemsdes.getString(2);

					HashMap<String, String> maptrdes = new HashMap<String, String>();
					maptrdes.put("item_result_1", item_result_1);
					maptrdes.put("item_result_2", item_result_2);
					maptrdes.put("item_result_3", item_result_3);

					// fill in the grid_item layout
					fillMapstrdes.add(maptrdes);
					SimpleAdapter adaptertrdes = new SimpleAdapter(this,
							fillMapstrdes,
							R.layout.dm_sales_summary_reporth_raw, fromtrdes,
							totrdes);
					lvtypedes.setAdapter(adaptertrdes);
				}
			}
		}
		allrowitemsdes.close();
		mydb.close();
	}

	private void currentuserTemp() {
		// TODO Auto-generated method stub
		SQLiteDatabase mydb;
		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor selectCurrentuserrec = null;
		try {
			selectCurrentuserrec = mydb.query(USER_SETTINGS, null, null, null,
					null, null, null);
		} catch (Exception e) {
		}
		if (selectCurrentuserrec != null) {
			if (selectCurrentuserrec.getCount() > 0) {
				while (selectCurrentuserrec.moveToNext()) {
					currentUserTemp = selectCurrentuserrec.getString(1);
					currentUserPrefix = selectCurrentuserrec.getString(2);
					UserLocCode = selectCurrentuserrec.getString(5);
					dmdisplayrepname.setText(currentUserTemp);
				}
			}
		}
		selectCurrentuserrec.close();
		mydb.close();
	}
	@Override
	public void onBackPressed() {
		// Do Here what ever you want do on back press;
	}
}
